package cn.iocoder.yudao.module.test.service;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi;
import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO;
import cn.iocoder.yudao.module.test.controller.admin.bpmPurchaseOrderDemo.vo.BpmPurchaseOrderDemoInstanceVO;
import cn.iocoder.yudao.module.test.dal.dataobject.bpmPurchaseOrderDemo.BpmPurchaseOrderDemoDO;
import cn.iocoder.yudao.module.test.dal.mysql.BpmPurchaseOrderDemoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author cjc
 * @Date 2024/1/12 10:49
 * @Description: 采购订单-业务service实现类
 * @Version 1.0
 */
@Service
public class BpmPurchaseOrderDemoServiceImpl implements BpmPurchaseOrderDemoService{


    public static final String PROCESS_KEY = "bpm_order";

    @Resource
    private BpmPurchaseOrderDemoMapper bpmPurchaseOrderDemoMapper;

    @Resource
    private BpmProcessInstanceApi processInstanceApi;

    @Override
    public Long createOrder(BpmPurchaseOrderDemoDO purchaseOrderDO){
        purchaseOrderDO.setUserId(SecurityFrameworkUtils.getLoginUserId());
        purchaseOrderDO.setOrderTime(LocalDateTime.now());
        purchaseOrderDO.setAuditStatus(1);
        bpmPurchaseOrderDemoMapper.insert(purchaseOrderDO);
        return purchaseOrderDO.getId();
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public String createBpm(BpmPurchaseOrderDemoInstanceVO instanceVO){

        BpmPurchaseOrderDemoDO orderDO = bpmPurchaseOrderDemoMapper.selectById(instanceVO.getOrderId());


        // 发起 BPM 流程
        Map<String, Object> processInstanceVariables = new HashMap<>();
        //业务参数
        processInstanceVariables.put("orderType",orderDO.getOrderType());
        processInstanceVariables.put("quantity", orderDO.getQuantity());
        processInstanceVariables.put("price", orderDO.getPrice());
        //流程参数
        processInstanceVariables.put("currency",instanceVO.getCurrency());
        processInstanceVariables.put("supplierName",instanceVO.getSupplierName());
        processInstanceVariables.put("isMaterial",instanceVO.getIsMaterial());
        processInstanceVariables.put("area",instanceVO.getArea());
        processInstanceVariables.put("star",instanceVO.getStar());
        processInstanceVariables.put("deliveryDate",instanceVO.getDeliveryDate());
        processInstanceVariables.put("deliveryTime",instanceVO.getDeliveryTime());

        String processInstanceId = processInstanceApi.createProcessInstance(SecurityFrameworkUtils.getLoginUserId(),
                new BpmProcessInstanceCreateReqDTO().setProcessDefinitionKey(PROCESS_KEY)
                        .setVariables(processInstanceVariables).setBusinessKey(String.valueOf(orderDO.getId())));

        // 将工作流的编号，更新到 业务单据中
        bpmPurchaseOrderDemoMapper.updateById(new BpmPurchaseOrderDemoDO().setId(orderDO.getId()).setProcessInstanceId(processInstanceId));
        return orderDO.getId() + "-" + processInstanceId;
    }

    @Override
    public void updateOrder(String businessKey, Integer result) {
        BpmPurchaseOrderDemoDO orderDO = new BpmPurchaseOrderDemoDO();
        orderDO.setId(Long.parseLong(businessKey));
        orderDO.setAuditStatus(result);
        bpmPurchaseOrderDemoMapper.updateById(orderDO);
    }
}
